package com.jkc.xh.fragments;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.jkc.xh.R;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.UserInfo;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_set_account)
public class AccountSetFragment extends BaseFragment {
    @ViewById
    NavBar nav_bar;
    @ViewById
    EditText inputRealName;
    @ViewById
    EditText inputIdCardNum;
    @ViewById
    EditText inputAddress;
    @ViewById
    EditText inputEmail;
    @ViewById
    EditText inputQQNumber;

    private UserInfo info;

    @AfterViews
    void initView() {
        nav_bar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {

            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }
        });

        showProgress("正在加载...");
        addJob(UserManager.getUserInfo(new RestCallBack<UserInfo>() {
            @Override
            public void success(UserInfo userInfo) {
                hideProgress();
                info = userInfo;
                inputRealName.setText(userInfo.real_name);
                inputIdCardNum.setText(userInfo.idno);
                inputAddress.setText(userInfo.address);
                inputEmail.setText(userInfo.email);
                inputQQNumber.setText(userInfo.qq);
            }

            @Override
            public void failure(RestError error) {
                hideProgress();
            }
        }));
    }

    @Click(R.id.btnSave)
    void saveChange() {
        String address = inputAddress.getText().toString();
        if (TextUtils.isEmpty(address) ) {
            ToastUtil.showToastShort("请输入地址信息");
            return;
        }
        String email = inputEmail.getText().toString();
        if (TextUtils.isEmpty(email) ) {
            ToastUtil.showToastShort("请输入有邮箱信息");
            return;
        }

        String idno = inputIdCardNum.getText().toString();
        if (TextUtils.isEmpty(idno) || idno.length() < 15) {
            ToastUtil.showToastShort("请输入正确的身份证号码");
            return;
        }

        String realName = inputRealName.getText().toString();
        if (TextUtils.isEmpty(realName)) {
            ToastUtil.showToastShort("请输入真实姓名");
            return;
        }

        String qq = inputQQNumber.getText().toString();
        if (TextUtils.isEmpty(qq)) {
            ToastUtil.showToastShort("请输入正确的QQ号");
            return;
        }
        info.address = address;
        info.email = email;
        info.idno = idno;
        info.real_name = realName;
        info.qq = qq;
        showProgress();
        addJob(UserManager.updateUserInfo(info, new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                hideProgress();
                ToastUtil.showToastShort("修改信息成功");
            }

            @Override
            public void failure(RestError error) {
                hideProgress();
                ToastUtil.showToastShort(error.msg);
            }
        }));
    }
}
